package com.rfid.emergencywarehouse.fragment;

import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.rfid.base.base.BasePresenter;
import com.rfid.emergencywarehouse.R;
import com.rfid.emergencywarehouse.adapter.KwAdapter;
import com.rfid.emergencywarehouse.entity.AssetOrderDetail;
import com.rfid.emergencywarehouse.entity.KW;
import com.rfid.emergencywarehouse.entity.KWAsset;
import com.rfid.emergencywarehouse.presenter.GetAssetWithOrderPresenter;
import com.rfid.emergencywarehouse.view.IGetAssetWithOrderView;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

public class GetAssetWithOrderFragment extends GetAssetFragment<GetAssetWithOrderPresenter> implements IGetAssetWithOrderView {

    @BindView(R.id.kw_recyclerView)
    RecyclerView mKWRecyclerView;
    @BindView(R.id.order_num)
    TextView order_num;
    @BindView(R.id.dlq)
    TextView dlq;
    @BindView(R.id.kf)
    TextView kf;

    List<KW> mKWS = new ArrayList<>();
    KwAdapter mKwAdapter;
    String OutOrderId;
    String FromWorkAreaId;

    @Override
    public GetAssetWithOrderPresenter getPresenter() {
        return new GetAssetWithOrderPresenter();
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        view.findViewById(R.id.with_order_parent).setVisibility(View.VISIBLE);
        Bundle bundle = getArguments();
        FromWorkAreaId = bundle.getString("FromWorkAreaId");
        OutOrderId = bundle.getString("orderId");
        System.out.println("OutOrderId = " + OutOrderId);
        mPresenter.getKW(OutOrderId,FromWorkAreaId);
        order_num.setText(bundle.getString("order_no"));
        dlq.setText("领取：" + bundle.getString("dlq"));
        kf.setText("库房：" + bundle.getString("kf"));
        mKWRecyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
        mKwAdapter = new KwAdapter(mKWS);
        mKWRecyclerView.setAdapter(mKwAdapter);
        setTitle("有单领取");
        mOrderId = OutOrderId;

        mPresenter.postEnterStatus(FromWorkAreaId,OutOrderId);
    }

    @Override
    public void onOrderDetail(AssetOrderDetail detail) {
        dlq.setText("领取：" + detail.getBarCodePickedCount() + "/" + detail.getBarCodeTotalCount());
    }

    @Override
    public void onDoGetAssetResult(boolean succes, String msg) {
        super.onDoGetAssetResult(succes, msg);
        if (succes) {
            mPresenter.getOrderDetail(mOrderId);
            mPresenter.getKW(OutOrderId, FromWorkAreaId);
        }
    }


    @Override
    public void onDestroyView() {
        super.onDestroyView();
        mPresenter.exitWorkArea(FromWorkAreaId,OutOrderId);
    }

    @Override
    public void onKWList(List<KW> kws) {
        mKWS.clear();
        mKWS.addAll(kws);
        mKwAdapter.notifyDataSetChanged();
    }
}
